<?php

action_gatekeeper('approvesvn');
$group_guid = (int)get_input('group_guid');
$message_guid = (int)get_input('message_guid');
$approved = (int)get_input('approved');

// get messages_guid
$message = get_entity($message_guid);
$group = get_entity($group_guid);

$repo = "/opt/lampp/htdocs/igs/svnrepositories/" . $group_guid;

// subject
$group_link = elgg_view('output/url', array(
    'text' => $group->name,
    'href' => 'groups/profile/' . $group_guid
));

$user = elgg_get_logged_in_user_entity();
$from = $user->guid;

$recipent = $message->fromId;

// get group_guid
if ($approved == "none") {
// if not -> disapprove -> send message to sender
    $message->is_svn_approved = 'disapproved';
    system_message('Đã từ chối cấp kho mã nguồn cho nhóm.');

    // subject
    $subject = elgg_echo('Yêu cầu tạo kho SVN đã bị từ chối');
    $group_text = 'Yêu cầu tạo kho SVN cho nhóm ' . $group_link . ' đã bị từ chối.';

} else {
// if have -> create repo, set permission, inform sender

    $exec = 'svnadmin create /opt/lampp/htdocs/igs/svnrepositories/' . $group_guid;
    $output = shell_exec($exec);

    // get group users
    $group_users = elgg_get_entities_from_relationship(array(
        'relationship' => 'member',
        'relationship_guid' => $group_guid,
        'inverse_relationship' => true,
        'types' => 'user',
        'limit' => 100,));

    $passwd_file = "[users]";
    $authz_file = "[groups]\nallaccess=";
    $i = 0;
    foreach ($group_users as $group_user) {
        if ($group_user->usertype == "Teacher") continue;
        // generate users' password
        $group_user->svn_password = generatePassword();

        //passwd
        $passwd_file .= "\n" . $group_user->username . "=" . $group_user->svn_password;

        //authz
        if ($i == 0) {
            $authz_file .= $group_user->username;
        } else {
            $authz_file .= ',' . $group_user->username;
        }

        // send message to each user
        $svn_auth_message_subject = elgg_echo('Thông báo tài khoản truy cập kho SVN');
        $svn_auth_message_body = 'Kho SVN của nhóm ' . $group_link . 'có địa chỉ ' . elgg_echo('groups:svnurlbase') . $group->getGUID() . '. ';
        $svn_auth_message_body .= ' Tài khoản truy cập: ' . $group_user->username . " / " . $group_user->svn_password;

        messages_send($svn_auth_message_subject, $svn_auth_message_body, $group_user->guid, $user->guid);

        $i++;
    }


    // write passwd file
    shell_exec("rm " . $repo . '/conf/passwd');
    file_put_contents($repo . '/conf/passwd', $passwd_file);

    //authz
    $authz_file .= "\n[/]\n*=\n@allaccess=rw";
    shell_exec("rm " . $repo . '/conf/authz');
    file_put_contents($repo . '/conf/authz', $authz_file);

    //authz
    $svnserve_file = "[general]\nanon-access=none\nauth-access=write\npassword-db=passwd\nauthz-db=authz\nrealm=" . $group->name;
    shell_exec('rm ' . $repo . '/conf/svnserve.conf');
    file_put_contents($repo . '/conf/svnserve.conf', $svnserve_file);

    // copy post-commit
    $exec = "rm /opt/lampp/htdocs/igs/svnrepositories/" . $group_guid . "/hooks/post-commit";
    shell_exec($exec);

    $exec = "cp /opt/lampp/htdocs/igs/svnrepositories/post-commit /opt/lampp/htdocs/igs/svnrepositories/" . $group_guid . "/hooks/";
    shell_exec($exec);

    $exec = "chmod +x /opt/lampp/htdocs/igs/svnrepositories/" . $group_guid . "/hooks/post-commit";
    shell_exec($exec);

    $message->is_svn_approved = 'approved';
    system_message('Đã cấp kho mã nguồn cho nhóm.');

    // subject
    $subject = elgg_echo('Yêu cầu tạo kho SVN đã được thông qua.');
    $group_text = 'Yêu cầu tạo kho SVN cho nhóm ' . $group_link . ' đã được thông qua.';

    $svn_link = 'Kho SVN của nhóm có địa chỉ ' . elgg_echo('groups:svnurlbase') . $group->getGUID() . '.';
    $group_text .= '<br/>';
    $group_text .= $svn_link;
}

// send it
$result = messages_send($subject, $group_text, $recipent, $from);

// check the sending
if (!$result) {
    register_error(elgg_echo("messages:error"));
}

//$message->is_svn_approved = null;
forward(REFERER);